package com.wxg.json;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.wxg.json.bean.Person;

/**
 * 
 * [](https://github.com/alibaba/fastjson/wiki/Samples-DataBind)
 * [](https://github.com/alibaba/fastjson/wiki/JSON_API_cn)
 * [](https://github.com/alibaba/fastjson/wiki/JSON_parseObject_InputStream_cn)
 * [](https://github.com/alibaba/fastjson/wiki/JSON_writeJSONString_cn)
 * [](https://github.com/alibaba/fastjson/wiki/JSONField)
 * [](https://github.com/alibaba/fastjson/wiki/JSONPath)
 * 
 * 
 * @author wxg
 *
 */
public class JsonTest {

	@Test
	public void test006(){
		String jsonfilename = "com/wxg/json/bean/person.json";
		String path = getClass().getClassLoader().getResource(jsonfilename).getPath();
		
		List<Person> persons = null;
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
			StringBuilder sbb = new StringBuilder();
			String line = null;
			while((line=br.readLine())!=null){
				sbb.append(line);
			}
			br.close();
			persons = JSON.parseArray(sbb.toString(), Person.class);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(null != persons){
			for(Person p : persons){
				System.out.println(p);
			}
		}
	}
	
	@Test
	@Ignore
	@Deprecated
	public void test005(){
		String jsonfilename = "com/wxg/json/bean/person.json";
		String path = getClass().getClassLoader().getResource(jsonfilename).getPath();
		
		List<Person> persons = null;
		
		try {
			FileInputStream fis = new FileInputStream(path);
			persons = JSON.parseObject(fis, new ArrayList<Person>().getClass());
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		if(null != persons){
			for(Person p : persons){
				System.out.println(p);
			}
		}
	}
	
	@Test
	@Ignore
	public void test004(){
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("wxg2", 322, "你好！2"));
		persons.add(new Person("Jack2", 222, "Hello"));
		persons.add(new Person("Mary2", 232, "Welocme!"));
		
		String jsonfilename = "com/wxg/json/bean/person.json";
		String path = getClass().getClassLoader().getResource(jsonfilename).getPath();
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(path);
			JSON.writeJSONString(fos, persons);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void test003(){
		Person person = new Person("Jack", 22, "What's matter? \"No matter\"");
		Assert.assertEquals("{\"age\":22,\"msg\":\"What's matter? \\\"No matter\\\"\","
				+ "\"name\":\"Jack\"}", JSON.toJSONString(person));
		Assert.assertEquals("What's matter? \"No matter\"", person.getMsg());
	}
	
	@Test
	public void test002(){
		List<Person> persons = new ArrayList<Person>();
		persons.add(new Person("wxg", 32, "你好！"));
		persons.add(new Person("Jack", 22, "Hello"));
		persons.add(new Person("Mary", 23, "Welocme!"));
		String jsonstr = JSON.toJSONString(persons);
		Assert.assertEquals("[{\"age\":32,\"msg\":\"你好！\",\"name\":\"wxg\"},"
				+ "{\"age\":22,\"msg\":\"Hello\",\"name\":\"Jack\"},"
				+ "{\"age\":23,\"msg\":\"Welocme!\",\"name\":\"Mary\"}]", jsonstr);
		
	}
	
	@Test
	public void test001(){
		Person person = new Person("wxg", 32, "你好！");
		String personStr = JSON.toJSONString(person);
		Assert.assertEquals("{\"age\":32,\"msg\":\"你好！\",\"name\":\"wxg\"}", personStr);
	}
	
	
}
